package us.icitap.timsportal.management;

import java.util.List;

import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.ColumnGenerator;

import us.icitap.timsportal.entity.User;
import us.icitap.timsportal.resources.TimsPortalLabels;
import us.icitap.timsportal.web.custom.LinkPortalButton;
import us.icitap.timsportal.web.custom.PortalPanel;

@SuppressWarnings("serial")
public class UserList extends PortalPanel{
	
	/**
	 * Natural property order for Person bean. Used in tables and forms.
	 */
	public static final Object[] NATURAL_COL_ORDER = new Object[] {
		"username", "officer", "function", "baseStructure" };

	/**
	 * "Human readable" captions for properties in same order as in
	 * NATURAL_COL_ORDER.
	 */
	
	public static final String[] COL_HEADERS_ENGLISH = new String[] {
		TimsPortalLabels.getLabel("label.username"),
		TimsPortalLabels.getLabel("label.officer"),
		TimsPortalLabels.getLabel("label.function"),
		TimsPortalLabels.getLabel("label.orgStructure") };
	
	private BeanItemContainer<User> dataSource;
	
	final LinkPortalButton newUserLink = new LinkPortalButton(TimsPortalLabels.getLabel("btn.newUser"));
	public Table table = new Table();
	final Label countLabel = new Label();
	
	public UserList(){
		setCaption(TimsPortalLabels.getLabel("title.userList"));
		setSizeFull();
		
		countLabel.setSizeFull();
		newUserLink.addStyleName("black");
		
		table.setSizeFull();
		dataSource = new BeanItemContainer<User>(User.class);
		table.setContainerDataSource(dataSource);
		
		table.setSelectable(true);
		table.setImmediate(true);
		
		table.setVisibleColumns(NATURAL_COL_ORDER);
		table.setColumnHeaders(COL_HEADERS_ENGLISH);
		
		table.addGeneratedColumn("officer", new ColumnGenerator() {
			@Override
			public Object generateCell(Table source, Object itemId,
					Object columnId) {
				String value;
				User u = (User) itemId;
				if(u.getOfficer() == null)
					value = u.getName()+ " " + u.getSurname();
				else value = u.getOfficer().getForename()+ " "+u.getOfficer().getSurname();
				
				return new Label(value);
			}
		});
		
		table.addGeneratedColumn("function", new ColumnGenerator() {
			@Override
			public Object generateCell(Table source, Object itemId,
					Object columnId) {
				String value;
				User u = (User) itemId;
				if(u.getOfficer() == null)
					value = u.getFunction();
				else value = u.getOfficer().getFunction();
				
				return new Label(value);
			}
		});
		
		table.addGeneratedColumn("baseStructure", new ColumnGenerator() {
			@Override
			public Object generateCell(Table source, Object itemId,
					Object columnId) {
				String value = "";
				User u = (User) itemId;
				if(u.getBaseStructure()!=null)
					value = u.getBaseStructure().getName();
				else if(u.getOfficer()!=null)
					u.getOfficer().getStructureName();
				else value = "";
				
//				if(u.getOfficer() == null && u.getBaseStructure() != null)
//					value = u.getBaseStructure().getName();
//				else value = u.getOfficer().getStructureName();
				
//				if(u.getOfficer() == null)
//					value = u.getStructure();
//				else value = u.getOfficer().getStructureName();
				
				return new Label(value);
			}
		});
		
		this.addComponent(newUserLink);
		this.addComponent(countLabel);
		this.addComponent(table);
	}
	
	@Override
	public void attach(){
		super.attach();
		this.table.addListener((ValueChangeListener) this.getApplication());
	}
	
	public void setDataSource(List<User> list){
		this.dataSource.removeAllItems();
		this.dataSource.addAll(list);
		this.countLabel.setValue("Gjithsej: "+String.valueOf(list.size())+" perdorues");
	}
	
}
